package com.forgenet.gate.config;

import com.forgenet.gate.session.GatePlayerSessionManager;
import com.forgenet.gate.socket.GateServerHandler;
import com.forgenet.gate.socket.GateServerInitializer;
import com.sparrow.common.EnableSparrowCommon;
import com.sparrow.common.perf.PerfTimerManager;
import com.sparrow.common.util.PropertiesUtil;
import com.sparrow.event.EnableSparrowEvent;
import com.sparrow.redis.EnableSparrowRedis;
import com.sparrow.rpc.EnableSparrowRpc;
import com.sparrow.rpc.manager.DistributeConsulRpcNodeManager;
import com.sparrow.servicemgr.consul.ConsulManager;
import com.sparrow.socket.http.EnableSparrowHttp;
import com.sparrow.socket.manager.PacketHandlerManager;
import com.sparrow.socket.tcp.EnableSparrowTcp;
import com.sparrow.socket.tcp.api.TcpServer;
import com.sparrow.socket.tcp.init.TcpServerInitializer;
import com.sparrow.socket.tcp.session.manager.GameSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableSparrowCommon
@EnableSparrowHttp
@EnableSparrowRpc
@EnableSparrowTcp
@EnableSparrowEvent
@EnableSparrowRedis
@ComponentScan({
        "com.forgenet.gate",
        "com.forgenet.rpc"
})
public class GateConfig {

    @Bean
    public ConsulManager consulManager() {
        ConsulManager consulManager = new ConsulManager();
        //注册网关提供给客户端的入口信息
        consulManager.register(PropertiesUtil.getProperty("node.group"), PropertiesUtil.getProperty("node.id"),
                PropertiesUtil.getProperty("node.ip"), PropertiesUtil.getPropertyAsInt("node.port"));

        //注册网关内部rpc服务信息
        consulManager.register(PropertiesUtil.getProperty("rpc.node.group"), PropertiesUtil.getProperty("rpc.node.id"),
                PropertiesUtil.getProperty("rpc.node.ip"), PropertiesUtil.getPropertyAsInt("rpc.node.port"));
        return consulManager;
    }

    @Bean
    public GatePlayerSessionManager gameSessionManager() {
        return new GatePlayerSessionManager();
    }

    @Bean
    public GateServerHandler gameServerHandler() {
        return new GateServerHandler();
    }

    @Bean
    public TcpServerInitializer tcpServer(GateServerHandler gameServerHandler) {
        GateServerInitializer serverInitializer = new GateServerInitializer();
        serverInitializer.setGameServerHandler(gameServerHandler);
        return serverInitializer;
    }

    @Bean
    public TcpServer nettyServer(TcpServerInitializer tcpServerInitializer) {
        TcpServer nettyServer = new TcpServer();
        nettyServer.setChannelInitializer(tcpServerInitializer);
        nettyServer.start(Integer.parseInt(PropertiesUtil.getProperty("node.port")));
        return nettyServer;
    }

    @Bean
    public DistributeConsulRpcNodeManager nodeMgr() {
        return new DistributeConsulRpcNodeManager();
    }

    @Bean
    public PerfTimerManager perfTimerRegistry() {
        return new PerfTimerManager();
    }

    @Bean
    public PacketHandlerManager packetHandlerManager() {
        return new PacketHandlerManager();
    }
}
